<?php

class PickerController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    private $_content = '';

    /*
     * Отображение секции
     */

    public function indexAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $model = new PageModel();
        $picker = new PickerModel();
        $snippets = new SnippetModel();
        $mods = $model->getAddonsList('page', 'title');
        $content = '';
        $section = $picker->getContent($sid);
        if ($section->pid > 0) {
            $pages = Zend_Registry::get('pages');
            $sections = $model->getSections($section->pid, true);
            $this->view->page = $model->getPages($section->pid);
            $i = 1;
            foreach ($sections as $key => $val) {
                if ($val->bid == 0) {
                    $module = $model->getAddons('page', $val->mid);

                    if ($module->title == 'picker' && $val->id == $sid) {
                        
                    } else {
                        $mod = ucfirst($module->title) . 'Controller';
                        $request = $this->getRequest();
                        $request->setParam('sid', $val->id);
                        $response = $this->getResponse();
                        $modaction = new $mod($request, $response, array('sid' => $val->id));
                        if ($module->title <> 'picker') {
                            $modaction->indexAction();
                            if ($val->sid <> 0) {
                                $snip = $snippets->getSnip($val->id);
                                if (!file_exists(APP_PATH . '/templates/snippets/' . $snip->file . '.phtml')) {
                                    $content = '';
                                } else {
                                    $this->view->setScriptPath(APP_PATH . '/templates/snippets');
                                    $this->_content .= $modaction->view->render($snip->file . '.phtml');
                                }
                            } else {
                                if (!file_exists(APP_PATH . '/templates/' . TEMPLATE_TITLE . '/scripts/' . $mods[$val->mid] . '/index.phtml')) {
                                    $this->view->setScriptPath(APP_PATH . '/templates/site/scripts');
                                }
                                $this->_content .= $modaction->view->render($mods[$val->mid] . '/index.phtml');
                            }
                        } else {
                            $this->_content .= $modaction->indexAction();
                        }
                    }
                }
            }
            return $this->_content;
        }
    }

    public function modifyAction() {
        $model = new PickerModel();
        $form = new PickerForm();
        $sid = (int) $this->getRequest()->getParam('id', 0);
        $pages_model = new PageModel();
        $section = $pages_model->getSection($sid);
        $this->view->form = $form->Picker(array('sid' => $sid, 'pid' => $section->pid));
        $this->view->section = $section;
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            $pid = (int) $formData['pid'];
            $content = array('pid' => $pid);
            if ($pid <> $section->pid) {
                $model->updateContent($content, $sid);
            }
            $this->_redirect('/pages/modify/id/' . $sid);
        } else {
            $form->populate(array('pid' => $model->getContent($sid)->pid));
        }
    }

    public function addAction() {
        $model = new PickerModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $model->addContent(array('sid' => $sid));
        $this->_redirect('/pages/section/id/' . $pid);
    }

}
